<?php
namespace common\components;

use Yii;
use yii\web\View as BaseView;
use yii\helpers\Html;
use common\widgets\Block;

/**
 * view类
 * Class View
 * @package common\components
 */
class View extends BaseView
{
    public $domain = '';
    public $level = 0;
    public $idx = 0;
    public $imghost = '';

    public function renderFile($viewFile, $params = [], $context = null)
    {
        $rs = parent::renderFile($viewFile, $params, $context);
        $this->level++;
        return $rs;
    }
    /**
     * 开始组件
     * @param $type string replace|append
     */
    public function beginBlock($id, $type = 'replace')
    {
        return Block::begin([
            'id' => $id,
            'type' => $type,
        ]);
    }

    /**
     * @param $id
     * @param $content
     * @param string $type
     */
    public function injectBlock($id, $content, $type = 'replace')
    {
        $this->blocks[$id][] = ['type' => $type, 'content' => $content, 'level' => $this->level, 'id'=>$this->idx++];
    }

    public function inline($id, $content)
    {
        $this->injectBlock($id, $content, 'replace');
    }

    public function endBlock()
    {
        Block::end();
    }

    public function get($key, $default = '')
    {
        return isset($this->params[$key]) ? $this->params[$key] : $default;
    }

    public function set($key, $value = '')
    {
        $this->params[$key] = $value;
    }

    /**
     * @param $position: header/footer
     */
    public function addJsFile($files, $id = 'jsFile')
    {
        if (is_string($files)) {
            $files = [$files];
        }
        foreach ($files as $file) {
            if (stripos($file, 'http') !== 0) {
                $file = $this->domain . $file;
            }

            $this->injectBlock($id, Html::jsFile($file), 'append');
        }
    }

    public function addCssFile($files, $id = 'cssFile')
    {
        $id = 'cssFile';
        if (is_string($files)) {
            $files = [$files];
        }
        foreach ($files as $file) {
            if (stripos($file, 'http') !== 0) {
                $file = $this->domain. $file;
            }

            $this->injectBlock($id, Html::cssFile($file), 'append');
        }
    }

    // 因为执行顺序的原因, block有问题,会导致file1 append, file2 append时, file2在file1前面
    public function block($name)
    {
        if (!empty($this->blocks[$name])) {
            $block = '';
            if (count($this->blocks[$name]) > 1) {
                usort($this->blocks[$name], function($a, $b) {
                    if ($a['level'] == $b['level']) {
                        return $a['id'] > $b['id'] ? 1 : -1;
                    }
                    return $a['level'] > $b['level'] ? -1 : 1; // level 倒序
                });
            }
            foreach ($this->blocks[$name] as $data) {
                switch ($data['type']) {
                    case 'replace':
                        if (trim($data['content'] != '')) {
                            $block = $data['content'];
                        }
                        break;
                    case 'append':
                        $block .= $data['content'];
                        break;
                    case 'insert':
                        $block = $data['content'] . $block;
                        break;
                }
            }
            echo $block;
        }
    }
}
